<template>
  <div>
    <el-container class="home_container">
      <el-header class="home_header">
        <div>
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="primary" @click="tuichu" style="height: 80%"
        >退出</el-button
        >
      </el-header>
      <el-container>
        <el-aside :width="isCollapse?'64px':'200px'" class="home_aside">
          <!-- 导航 -->
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="false"
             :collapse="isCollapse"
              :collapse-transition="false"
              router
          >
            <div class="toggle" @click="toggleCollapse">|||</div>
            <!--一级菜单  -->
            <el-submenu :index="item.id.toString()" v-for="item in menList" :key="item.id">
              <!-- 模板 -->
              <template slot="title">
                <i :class="iconObject[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+item1.path" v-for='item1 in item.children' :key="item1.id"
               @click="savePath('/'+item1.path)"
              >
                 <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 名字 -->
                <span>{{item1.authName}}</span>
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main class="home_main">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data () {
    return {
      menList: [],
      iconObject: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  async created () {
    // 获取数据
    const { data: res } = await this.$http.get('menus')
    console.log(res)
    if (res.meta.status === 200) {
      this.menList = res.data
    } else {
      return this.$message.error(res.meta.msg)
    }
    // 获取路由路径赋值
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    tuichu () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
      console.log(this.isCollapse)
    },
    // 保存路由跳转的路径
    savePath (path) {
      window.sessionStorage.setItem('activePath', path)
      // 点回去赋值
      this.activePath = path
    }

  }
}
</script>
<style scoped>
.home_container {
  height: 100vh;
}
.home_header {
  padding-left: 0;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.home_header div {
  color: white;
  display: flex;
  align-items: center;
}
.home_header div span {
  padding-left: 10px;
  font-size: 20px;
}
.home_aside {
  background-color: #333744;
}
.home_aside .el-menu{
  border-right: none;
}
.home_aside .iconfont{
  padding-right: 5px;
}
.home_aside .toggle{
  color: white;
  text-align: center;
  letter-spacing: 2px;
  background-color: #56585c ;
  height: 30px;
  line-height: 30px;
  cursor: pointer;
}
.home_main {
  background-color: #eaedf1;
}
</style>>
